package com.ezgzone.webcrawler;

import java.io.File;
import java.io.IOException;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.sqrq.DocumentX;
import com.sqrq.JsoupX;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileAppender;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;

public class ImageDownLmjxBrand {
	public static void main(String[] args) throws IOException {
		File imageSavedDir = FileUtil.file("C:\\temp\\brand_img");
		FileUtil.mkdir(imageSavedDir);
		String url = "http://brand.lmjx.net";
		DocumentX doc = JsoupX.connect(url).timeout(50000).get();
		String title = doc.doc().title();
		System.out.println("title: " + title);

		Elements els = doc.byTag("a").byAttr("class", "img").currentEles();

		System.out.println("Getting all images[" + els.size() + "]...");
		// System.out.println(els.html());

		File fileSql = FileUtil.file(imageSavedDir, "wm_machine_brand.sql");
		String templateSql = "INSERT INTO `wm_machine_brand`(`brand_id`, `brand_name`, `manufacturer`, `pinyin`, `sort`, `pic`, `created_at`, `updated_at`) "
				+ "VALUES ('{}','{}','{}','{}',0,'{}','2020-12-15 00:00:00','2020-12-15 00:00:00');";
		FileAppender appender = new FileAppender(fileSql, 16, true);
		int i = 0;
		for (Element eImg : els) {
			i++;
			// System.out.println(eImg.attr("title") + "->" + eImg.attr("abs:href"));
			String imgSrc = eImg.getElementsByTag("img").get(0).absUrl("lz_src");
			Element a = eImg.nextElementSibling();
			String manufacturer = a.attr("title");
			String temp1[] = a.attr("href").split("/");
			String temp2[] = temp1[temp1.length - 1].split("_");
			String pinyin = temp2[temp2.length - 1];
			String brandName = a.text();
			String strPrint = StrUtil.format("brandName:{},pinyin:{},manufacturer:{},imgSrc:{}", brandName, pinyin,
					manufacturer, imgSrc);
			System.out.println(strPrint);
			File destFile = FileUtil.file(imageSavedDir, pinyin + ".jpg");
			long size = HttpUtil.downloadFile(imgSrc, destFile);
			// long size = HttpUtil.downloadFile(fileUrl, FileUtil.file("e:/"));
			System.out.println("Download size: " + size);

			// sql
			String strSql = StrUtil.format(templateSql, i, brandName, manufacturer, pinyin,
					"attachment/machine/brand/" + pinyin + ".jpg");
			appender.append(strSql);
			appender.flush();
			appender.toString();
		}
	}

}
